package com.study.guide.codec.protobuf;


import com.study.guide.codec.protobuf.proto.SubscribeReqProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class TimeClientHandler extends SimpleChannelInboundHandler<Object> {


	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("Receive server response : [ " + msg+" ]");
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		for (int i = 0; i < 10; i++) {
			ctx.write(req(i+1));
		}
		ctx.flush();
	}


	private SubscribeReqProto.SubscribeReq req(int i){
		SubscribeReqProto.SubscribeReq.Builder builder =
				SubscribeReqProto.SubscribeReq.newBuilder();
		builder.setSubReqId(i)
				.setUserName("Lilinfeng")
				.setProductName("Netty book for protobuf");
		builder.setAddress("nanjing");
		return builder.build();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		ctx.close();
	}
}
